/*
 * Project: JDiveLog: A Dive Logbook written in Java
 * File: Mix.java
 * 
 * @author Pascal Pellmont <jdivelog@pellmont.dyndns.org>
 * 
 * This file is part of JDiveLog.
 * JDiveLog is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * JDiveLog is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with JDiveLog; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package scubalogbook.data.imports.jdivelog.model.gasblending;

/**
 * Represents a gas Mix
 * 
 * @author Pascal Pellmont <jdivelog@pellmont.dyndns.org>
 */
public class Mix {
    
    double oxygen;
    double nitrogen;
    double helium;
    
    public Mix() {
    }
    
    public Mix(int partO2, int partN2, int partHe) {
        double factor = 1 / (double)(partO2 + partN2 + partHe);
        oxygen = ((double)Math.round(partO2*factor*1000))/1000;
        nitrogen = ((double)Math.round(partN2*factor*1000))/1000;
        helium = ((double)Math.round(partHe*factor*1000))/1000;
    }

    public Mix(double partO2, double partN2, double partHe) {
        double factor = 1 / (partO2 + partN2 + partHe);
        oxygen = ((double)Math.round(partO2*factor*1000))/1000;
        nitrogen = ((double)Math.round(partN2*factor*1000))/1000;
        helium = ((double)Math.round(partHe*factor*1000))/1000;
    }

    public void setHelium(double he) {
        helium = he;
    }
    public double getHelium() {
        return helium;
    }

    public void setNitrogen(double n2) {
        nitrogen = n2;
    }
    public double getNitrogen() {
        return nitrogen;
    }

    public void setOxygen(double o2) {
        oxygen = o2;
    }
    public double getOxygen() {
        return oxygen;
    }
    
    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("<Mix>");
        sb.append("<oxygen>");
        sb.append(oxygen);
        sb.append("</oxygen>");
        sb.append("<nitrogen>");
        sb.append(nitrogen);
        sb.append("</nitrogen>");
        sb.append("<helium>");
        sb.append(helium);
        sb.append("</helium>");
        sb.append("</Mix>");
        return sb.toString();
    }
    
    @Override
    public boolean equals(Object other) {
        if (other instanceof Mix) {
            Mix om = (Mix)other;
            return om.getOxygen() == oxygen && om.getNitrogen() == nitrogen && om.getHelium() == helium;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        StringBuffer sb = new StringBuffer();
        sb.append(oxygen);
        sb.append(nitrogen);
        sb.append(helium);
        return sb.toString().hashCode();
    }
    
    @Override
    public Mix clone() {
        return new Mix(oxygen, nitrogen, helium);
    }

}
